#include "Bullet.h"
#include "ObjectID.h"
#include "MathUtil.h"
#include "Config.h"


Bullet::Bullet(void)
{
	_velocity = ccp(0, 0);
	_acceleration = CCPointZero;
	_damage = 0;
	_isShot = false;
	_isRotateByDirect = true;
	_IdWave = NULL;
	_id = NULL;
}


Bullet::~Bullet(void)
{
	CC_SAFE_RELEASE(_IdWave);
	CC_SAFE_RELEASE(_id);
}

bool Bullet::init()
{
	if (!BaseObject::init())
		return false;

	
	return true;
}

void Bullet::updatePosition( float dt )
{
	//this->setPositionX(this->getPositionX() + _velocity.x * dt + 1 / 2.0f * _acceleration.x * dt * dt);
	//this->setPositionY(this->getPositionY() + _velocity.y * dt + 1 / 2.0f * _acceleration.y * dt * dt);
	this->setPosition(getPosition() + _velocity * dt + _acceleration * (1 / 2.0f) * dt * dt);

	this->setVelocity(_velocity + _acceleration * dt);
}

void Bullet::onShot(BaseObject* enemy)
{
	_isShot = true;
}

void Bullet::reset()
{
	_isShot = false;
	_isRotateByDirect = true;
// 	CCParticleSystem* pa = dynamic_cast<CCParticleSystem*>(getChildByTag(TAG_PARTICLE));
// 	if (pa)
// 	{
// 		particle->unscheduleUpdate();
// 		particle->scheduleUpdate();
// 	}
}

CCPoint Bullet::getVelocity()
{
	return _velocity;
}

void Bullet::setVelocity(CCPoint var)
{
	_velocity = var;
	if (_isRotateByDirect)
		updateDirect();
}

void Bullet::updateDirect()
{
	float angle = MathUtil::angle2Vetor(DEFAULT_DIRECT, _velocity);
	//this->setRotation(MathUtil::RadToAngleDegree(-angle));
	CCArray* listChildren = getChildren();
	CCObject* it;
	CCARRAY_FOREACH(listChildren, it)
	{
		CCNode* child = dynamic_cast<CCNode*>(it);
		if (child->getTag() != TAG_PARTICLE)
			child->setRotation(-MathUtil::RadToAngleDegree(angle));
	}
}

void Bullet::onEnter()
{
	BaseObject::onEnter();
	CCArray* listChildren = getChildren();
// 	CCObject* it;
// 	CCARRAY_FOREACH(listChildren, it)
// 	{
// 		CCNode* child = dynamic_cast<CCNode*>(it);
// 		if (child->getTag() == TAG_PARTICLE)
// 		{
// 			dynamic_cast<CCParticleSystem*>(child)->unscheduleUpdate();
// 			dynamic_cast<CCParticleSystem*>(child)->scheduleUpdate();
// 		}
// 	}
}

void Bullet::onExit()
{
	BaseObject::onExit();
}
